package com.zwr.sheller.sso.controller;

import com.zwr.sheller.core.common.JsonResult;
import com.zwr.sheller.core.common.WQQBusinessException;
import com.zwr.sheller.core.pojo.User;
import com.zwr.sheller.sso.Service.LoginService;
import com.zwr.sheller.sso.Service.SmsService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author: 18032
 * @Date: 17:33 2022/1/10
 */
@RestController
@RequestMapping("/sso")
@CrossOrigin
public class SsoController {
    @Autowired
    SmsService smsService;
    @Autowired
    LoginService loginService;

    @PostMapping("/send")
    @ApiOperation("发送验证码接口")
    public Map<String, Object> sendSMS(String phone) {
        String s = smsService.sendLoginSms(phone);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("发送成功");
        jsonResult.setField("code", s);
        return jsonResult.getMap();
    }

    @PostMapping("/login")
    @ApiOperation("登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "pwd", value = "密码", dataType = "String", paramType = "query", required = true),
    })
    public Map<String, Object> login(String phone, String pwd) throws WQQBusinessException {
        Map<String, Object> login = loginService.login(phone, pwd);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("发送成功");
        jsonResult.setField("data", login);
        return jsonResult.getMap();
    }

    @PostMapping("/checkToken")
    public Map<String, Object> login(String token) throws WQQBusinessException {
        User user = loginService.checkToken(token);
        System.out.println("user:" + user);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("发送成功");
        jsonResult.setField("data", user);
        return jsonResult.getMap();
    }

    @PostMapping("/forgetPwd")
    @ApiOperation("忘记密码?")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "pwd", value = "密码", dataType = "String", paramType = "query", required = true)
    })
    public Map<String, Object> forgetPwd(String phone, String code, String pwd) throws WQQBusinessException {
        Integer integer = loginService.forgetPwd(phone, code, pwd);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }

    @PostMapping("addNewUser")
    @ApiOperation("注册用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userPhone",value = "注册手机号",dataType = "String",paramType = "query",required = true),
            @ApiImplicitParam(name = "userPwd",value = "密码",dataType = "String",paramType = "query",required = true),
            @ApiImplicitParam(name = "userName",value = "用户昵称",dataType = "String",paramType = "query",required = true),
            @ApiImplicitParam(name = "userRetailName",value = "店铺名称",dataType = "String",paramType = "query",required = true),
            @ApiImplicitParam(name = "userPrincipleName",value = "负责人姓名",dataType = "String",paramType = "query",required = true),
            @ApiImplicitParam(name = "userPrinciplePhone",value = "负责人手机号",dataType = "String",paramType = "query",required = true),
            @ApiImplicitParam(name = "code",value = "验证码",dataType = "String",paramType = "query",required = true)
    })
    public Map<String, Object> addNewUser(User user,String code) throws WQQBusinessException {
        Integer integer = loginService.addNewUser(user,code);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("注册成功");
        return jsonResult.getMap();

    }
}
